package cn.edu.scnu.comment.controller;

import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.comment.service.CommentService;
import cn.edu.scnu.pojo.Comment;
import cn.edu.scnu.pojo.User;
import cn.edu.scnu.utils.LoginUtils;
import cn.edu.scnu.vo.CommentVO;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@Api
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Lazy
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/comments")
    @ApiOperation("保存评论")
    @ApiImplicitParam(
            name = "comment",
            value = "评论信息",
            dataType = "cn.edu.scnu.pojo.Comment",
            required = true,
            type = "body"
    )
    public SysResult saveComment(@RequestBody Comment comment) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        comment.setFromId(user.getId());
        comment.setFromName(user.getUsername());
        commentService.saveComment(comment);
        return SysResult.build(ResultCode.CREATED, "save success", null);
    }

    @IgnoreToken
    @GetMapping("/comments/matchs/{matchId}")
    @ApiOperation("查看某个比赛的所有评论")
    @ApiImplicitParam(
            name = "matchId",
            value = "比赛Id",
            dataType = "String",
            required = true,
            type = "path"
    )
    public SysResult getCommentByMatchId(@PathVariable String matchId) {
        List<CommentVO> data = commentService.getCommentByMatchId(matchId);
        return SysResult.build(ResultCode.SUCCESS, "success", data);
    }

    @PatchMapping("/comments/{commentId}/likenum")
    @ApiOperation("点赞功能")
    @ApiImplicitParam(
            name = "commentId",
            value = "评论id",
            dataType = "String",
            required = true,
            type = "path"
    )
    public SysResult likeComment(@PathVariable String commentId) {
        commentService.likeComment(commentId);
        return SysResult.build(ResultCode.CREATED, "like success", null);
    }

    @DeleteMapping("/comments/{commentId}")
    @ApiOperation("删除某一条评论")
    @ApiImplicitParam(
            name = "commentId",
            value = "评论id",
            dataType = "String",
            required = true,
            type = "path"
    )
    public SysResult delCommentById(@PathVariable String commentId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        if (commentService.delCommentById(user.getUsername(), commentId) > 0) {
            return SysResult.build(ResultCode.NO_CONTENT, "delete success", null);
        }
        return SysResult.build(ResultCode.FAIL, "error", null);
    }

    @DeleteMapping("/comments/matchs/{matchId}")
    @ApiOperation("删除某个比赛的所有评论")
    @ApiImplicitParam(
            name = "matchId",
            value = "比赛Id",
            dataType = "String",
            required = true,
            type = "path"
    )
    public SysResult delCommentByMatchId(@PathVariable String matchId) {
        User user = LoginUtils.getUserInfo();
        if (user.getType() == 0) {
            commentService.delCommentMatchId(matchId);
            return SysResult.build(ResultCode.NO_CONTENT, "delete success", null);
        } else if (user.getType() == 1) {
            SysResult result = restTemplate.getForObject("http://matchService/matchs/" + matchId, SysResult.class);
//            MatchActivity match = null;
            Map<String, String> match = null;
            if (result.getStatus() == 200) {
                match = (Map<String, String>) result.getData();
            }
            if (match != null && user.getUsername().equals(match.get("orgName"))) {
                commentService.delCommentMatchId(matchId);
                return SysResult.build(ResultCode.NO_CONTENT, "delete success", null);
            }
        }

        return SysResult.build(ResultCode.FAIL, "权限错误！", null);
    }
}
